//
//  MonthButton.m
//  MonthView
//
//  Created by  ibokan on 10-8-23.
//  Copyright 2010 tencent.com. All rights reserved.
//

#import "MonthButton.h"
#import "NSDate-Helper.h"
#import "SmartRoadShowAppDelegate.h"
#import <QuartzCore/QuartzCore.h>
#import "CalendarViewController.h"
#import "DayDetailViewController.h"
#import "ArtTour.h"
#import "ArtRoadshowTour.h"
#import "ArtEvent.h"

@implementation MonthButton
@synthesize buttonDate;
@synthesize artEventTag,artRoadshowTag,artTourTag;



//是本月的日期时触发的方法
-(void) pressed
{
	if (able) {
		SmartRoadShowAppDelegate *delegate = (SmartRoadShowAppDelegate *)[[UIApplication sharedApplication] delegate];
		DayDetailViewController *dayDetail = [[[DayDetailViewController alloc] init] autorelease];
		dayDetail.calenderVC = delegate._calendarViewController;
		
		[delegate._calendarViewController.navigationController pushViewController:dayDetail animated:YES];
		
		[dayDetail setSelectDate:buttonDate];
		
		NSString *dateString = [NSDate stringFromDate:buttonDate];
		array1 = [ArtEvent searchAllArtEvents:dateString];
    	
		array2 = [ArtTour searchAllArtTour:dateString];
		
		
		array3 = [ArtRoadshowTour searchAllArtRoadshowTour:dateString];
		
		dayDetail.artEventArray = array1;
		dayDetail.artTourArray = array2 ;
		dayDetail.artRoadshowArray = array3;	
		
	}
}

-(void) goToPreMonth
{
	SmartRoadShowAppDelegate *delegate = (SmartRoadShowAppDelegate *)[[UIApplication sharedApplication] delegate];
	[delegate._calendarViewController._scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

-(void) goToNextMonth
{
	SmartRoadShowAppDelegate *delegate = (SmartRoadShowAppDelegate *)[[UIApplication sharedApplication] delegate];
	[delegate._calendarViewController._scrollView setContentOffset:CGPointMake(640, 0) animated:YES];
}

- (void)setButtonWithMonthAgo:(int)monthTag
{
	
	self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
	switch (monthTag)
	{
		case 0:
			[self addTarget:self action:@selector(pressed) forControlEvents:UIControlEventTouchDown];
			break;
		case 1:
			[self setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
			break;
		case -1:
			[self setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
			break;
		default:
			break;
	}
	
}


- (id)initButtonWithDate:(NSDate *)date Frame:(CGRect)aframe isNowMonth:(int) monthTag
{
	if (self = [super init]) {
		
		able = NO;
		
		self.frame = aframe;
		buttonDate = [date retain];
		[self  setTitle:[NSString stringWithFormat:@"%d",[date getDay]] forState:UIControlStateNormal];
		if ([date getDay] == [[NSDate date] getDay]&&[date getMonth] == [[NSDate date] getMonth] && [date getYear] == [[NSDate date] getYear]) 
		{
			[self  setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
			[self setBackgroundImage:[UIImage imageNamed:@"todayBG"] forState:UIControlStateNormal];
			self.layer.cornerRadius = 5;
			self.layer.masksToBounds = YES;
		}
		else 
		{
			[self  setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
		}
		[self.titleLabel setFont:[UIFont fontWithName:@"Verdana-Bold" size:18]];
		[self  setButtonWithMonthAgo:monthTag];
		
		pointCount = 0;
		
		if (monthTag == 0) {
			NSString *bDateString = [NSDate stringFromDate:buttonDate];
			if ([ArtEvent IsExistArtEvent:bDateString]) {
				point0 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 6, 6)];
				[point0 setContentMode:UIViewContentModeScaleAspectFit];
				[point0 setImage:[UIImage imageNamed:@"point2.png"]];
				[point0 setContentMode:UIViewContentModeCenter];
				[self addSubview:point0];
				
				pointCount++;
				able = YES;
			}
			
			if ([ArtRoadshowTour IsExistArtRoadshowTour:bDateString]) {
				point1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 6, 6)];
				[point1 setContentMode:UIViewContentModeScaleAspectFit];
				[point1 setImage:[UIImage imageNamed:@"point1.png"]];
				[point1 setContentMode:UIViewContentModeCenter];
				[self addSubview:point1];
				
				pointCount++;
				able = YES;
			}
			
			if ([ArtTour IsExistArtTour:bDateString]) {
				point2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 6, 6)];
				[point2 setContentMode:UIViewContentModeScaleAspectFit];
				[point2 setImage:[UIImage imageNamed:@"point1.png"]];
				[point2 setContentMode:UIViewContentModeCenter];
				[self addSubview:point2];
				
				pointCount++;
				able = YES;
			}
			
			switch (pointCount) {
				case 1:
				{
					if (point0 != nil) {
						[point0 setCenter:CGPointMake(20, 30)];
					}
					if (point1 != nil) {
						[point1 setCenter:CGPointMake(20, 30)];
					}
					if (point2 != nil) {
						[point2 setCenter:CGPointMake(20, 30)];
					}
					break;
				}
				case 2:
				{
					if (point0 != nil) {
						[point0 setCenter:CGPointMake(16, 30)];
						if (point1 != nil) {
							[point1 setCenter:CGPointMake(24, 30)];
						}else if (point2 != nil) {
							[point2 setCenter:CGPointMake(24, 30)];
						}
					}
					else {
						[point1 setCenter:CGPointMake(16, 30)];
						[point2 setCenter:CGPointMake(24, 30)];
					}
					
					break;
				}
				case 3:{
					[point0 setCenter:CGPointMake(13, 30)];
					[point1 setCenter:CGPointMake(20, 30)];
					[point2 setCenter:CGPointMake(27, 30)];
					break;
				}
					
				default:
					break;
			}
			
		}
		
		}
	return self;
}


- (void)dealloc 
{
    [super dealloc];
}

@end
